package de.jmda.gen.java.impl;

import static org.junit.Assert.assertTrue;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.junit.Test;

import de.jmda.gen.GeneratorException;
import de.jmda.gen.LineIndenter;
import de.jmda.gen.java.DeclaredInstanceMethodGenerator;
import de.jmda.gen.java.InstanceMethodGenerator;
import de.jmda.gen.java.MethodGenerator;

public class JUTDefaultMethodDeclarationGenerator
{
	private final static Logger LOGGER =
      Logger.getLogger(JUTDefaultMethodDeclarationGenerator.class);

	@Test
	public void testDefaultInstanceMethodDeclarationGenerator()
			throws GeneratorException
	{
		MethodGenerator generator =
				new DefaultInstanceMethodGenerator(
						new DefaultDeclaredInstanceMethodGenerator());

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated" + System.lineSeparator() + "[" + generated + "]");

		assertTrue(
				"unexpected method declaration" + System.lineSeparator() +
						"[" + generated + "]",
				(
						"void method_name()" + System.lineSeparator() +
						"{"                  + System.lineSeparator() +
						"}"
				).contentEquals(generated));
	}

	@Test
	public void testDefaultInstanceMethodDeclarationGeneratorWithIndentation()
			throws GeneratorException
	{
		MethodGenerator generator =
				new DefaultInstanceMethodGenerator(
						new DefaultDeclaredInstanceMethodGenerator());
		
		String indentation = "x";
		int repeat = 3;
		
		LineIndenter indenter = new LineIndenter(indentation, repeat);

		// TODO set line indenter
		generator.setLineIndenter(indenter);

		String indentationResult = StringUtils.repeat(indentation, repeat);

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated" + System.lineSeparator() + "[" + generated + "]");

		assertTrue(
				"unexpected method declaration" + System.lineSeparator() +
						"[" + generated + "]",
				(
						indentationResult + "void method_name()" + System.lineSeparator() +
						indentationResult + "{"                  + System.lineSeparator() +
						indentationResult + "}"
				).contentEquals(generated));
	}

	@Test
	public void testDefaultStaticMethodDeclarationGenerator()
			throws GeneratorException
	{
		MethodGenerator generator =
				new DefaultStaticMethodGenerator(
						new DefaultDeclaredStaticMethodGenerator());

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated [" + generated + "]");

		assertTrue(
				"unexpected method declaration [" + generated + "]",
				(
						"static void method_name()" + System.lineSeparator() +
						"{"                         + System.lineSeparator() +
						"}"
				).contentEquals(generated));
	}

	@Test
	public void testDefaultMethodDeclarationGeneratorWithJavaDoc()
			throws GeneratorException
	{
		String input1 = "test1";
		String input2 = "test2";

		MethodGenerator generator =
				new DefaultInstanceMethodGenerator(
						new DefaultDeclaredInstanceMethodGenerator(
								new DefaultMethodModifiersGenerator(),
								new DefaultTypeNameGenerator(),
								new DefaultMethodNameGenerator(),
								new DefaultMethodParametersGenerator(),
								new DefaultThrowsClauseGenerator(),
								new DefaultMethodBodyGenerator()));

		generator.setJavaDocGenerator(new DefaultJavaDocGenerator(input1, input2));

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated [" + generated + "]");

		assertTrue(
				"unexpected method declaration [" + generated + "]",
				(
						"/**"                + System.lineSeparator() +
						" * " + input1       + System.lineSeparator() +
						" * " + input2       + System.lineSeparator() +
						" */"                + System.lineSeparator() +
						"void method_name()" + System.lineSeparator() +
						"{"                  + System.lineSeparator() +
						"}"
				).contentEquals(generated));
	}

	@Test
	public void testDefaultInterfaceMethodDeclarationGenerator()
			throws GeneratorException
	{
		DeclaredInstanceMethodGenerator g =
				new DefaultDeclaredInstanceMethodGenerator();
		
		g.setInterfaceMethodGenerator(true);

		InstanceMethodGenerator generator =
				new DefaultInstanceMethodGenerator(g);

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated" + System.lineSeparator() + "[" + generated + "]");

		assertTrue(
				"unexpected method declaration" + System.lineSeparator() +
						"[" + generated + "]",
						"void method_name();".contentEquals(generated));
	}
}